<template>
  <div
    class="draw"
    v-if="drawDialogVisible"
    ref="drawRef"
    :class="dialogClassName"
    @mouseenter="newDra"
    @click="selectDialog"
  >
    <div class="draw-con" v-if="detailData">
      <div v-show="!detailModel">
        <div class="draw-con-top">
          <img src="@/assets/img/home/dialog_menu.png" alt="" @click="clickDetail" />
          <img src="@/assets/img/home/icon_close.png" alt="" @click="closeDialog" />
        </div>
        <div class="draw-con-title">
          <p class="title-brand">{{ detailData.info.brand_name }}</p>
          <p class="title-serial">设备序列号：{{ detailData.info.serial_no }}</p>
          <img src="@/assets/img/home/dialog_play.png" @click="openSingleScreen" alt="" />
        </div>
        <div class="draw-con-line"></div>
        <div class="draw-con-main">
          <div class="draw-con-main-left">
            <div class="postion-top">
              <div class="postion-top-left">
                <p style="color: #fff">当前位置</p>
                <div class="postion-latlng">
                  <img src="@/assets/img/home/lat_purple.png" alt="" />
                  <p>纬度</p>
                  <span>{{ detailData.info.point.lat }}</span>
                </div>
              </div>
              <div class="postion-top-right">
                <p class="postion-title" v-if="locationCurrent.addressComponent">
                  <span
                    >{{
                      locationCurrent.addressComponent.city
                        ? locationCurrent.addressComponent.city
                        : locationCurrent.addressComponent.province
                    }}-{{ locationCurrent.addressComponent.district }} </span
                  ><b @click="clickPostion(1)">定位</b>
                </p>
                <p v-else style="color: #fff">暂无位置信息</p>
                <div class="postion-latlng">
                  <img src="@/assets/img/home/lat_green.png" alt="" />
                  <p>经度</p>
                  <span>{{ detailData.info.point.lon }}</span>
                </div>
              </div>
            </div>
            <div class="postion-bottom" style="margin-top: 25px" v-if="detailData.info">
              <div class="postion-bottom-left">
                <p style="color: #fff">登记位置</p>
                <div class="postion-latlng">
                  <img src="@/assets/img/home/icon_lat.png" alt="" />
                  <p>纬度</p>
                  <span>{{ detailData.info.register_point.lat }}</span>
                </div>
              </div>
              <div class="postion-bottom-right">
                <p class="postion-title" v-if="locationReg.addressComponent">
                  <span
                    >{{
                      locationReg.addressComponent.city
                        ? locationReg.addressComponent.city
                        : locationReg.addressComponent.province
                    }}-{{ locationReg.addressComponent.district }} </span
                  ><b @click="clickPostion(2)">定位</b>
                </p>
                <p v-else style="color: #fff">暂无位置信息</p>
                <div class="postion-latlng">
                  <img src="@/assets/img/home/icon_lng.png" alt="" />
                  <p>经度</p>
                  <span>{{ detailData.info.register_point.lon }}</span>
                </div>
              </div>
            </div>
          </div>
          <div class="draw-con-main-right" v-if="detailData.info">
            <div class="draw-con-main-right-left">
              <p>设备状态</p>
              <p>所属单位</p>
              <p>联系人</p>
              <p>联系人电话</p>
            </div>
            <div class="draw-con-main-right-right">
              <p>{{ detailData.info.device_status }}</p>
              <p :title="detailData.info.unit_name">{{ detailData.info.unit_name }}</p>
              <p>{{ detailData.info.contact_user }}</p>
              <p>{{ detailData.info.contact_mobile }}</p>
            </div>
          </div>
        </div>
      </div>
      <!-- 详情信息 -->
      <div v-if="detailModel">
        <div v-if="monitorDetail.data">
          <div class="draw-con-back" @click="clickBack">
            <img src="@/assets/img/home/icon_back.png" alt="" />
            <p>返回</p>
          </div>
          <div class="draw-detail">
            <div class="draw-detail-task">
              <p>{{ monitorDetail.data.data.survey_area_name }}</p>
              <div class="task-device">
                <div class="task-device-item">
                  <div class="task-device-item-img">
                    <img src="@/assets/img/home/icon_fly.png" alt="" style="margin-right: 8px" />
                    <div>
                      <span class="draw-grey">设备型号</span>
                      <p class="draw-blue">{{ monitorDetail.data.data.plane_type }}</p>
                    </div>
                  </div>
                </div>
                <div class="task-device-item" style="margin-top: 30px">
                  <div class="task-device-item-img">
                    <img src="@/assets/img/home/icon_load.png" alt="" style="margin-right: 8px" />
                    <div>
                      <span class="draw-grey">载荷</span>
                      <p class="draw-blue">{{ monitorDetail.data.data.camera_type }}</p>
                    </div>
                  </div>
                </div>
              </div>
            </div>
            <!-- 比例尺 -->
            <div class="draw-detail-scale">
              <div class="scale-item">
                <p style="color: #fff">比例尺</p>
                <p class="draw-blue">{{ monitorDetail.data.data.work_scale }}</p>
              </div>
              <ul style="margin-top: 10px">
                <li v-for="(item, index) in scaleInfoTop" :key="index">
                  <div class="scale-item">
                    <p class="draw-grey">{{ item.label }}</p>
                    <p class="draw-blue">{{ monitorDetail.data.data[item.key] }}{{ item.unit }}</p>
                  </div>
                </li>
              </ul>
              <ul style="margin-top: 20px">
                <li v-for="(item, index) in scaleInfoBot" :key="index">
                  <div class="scale-item">
                    <p class="draw-grey">{{ item.label }}</p>
                    <p class="draw-blue">{{ monitorDetail.data.data[item.key].toFixed(1) }}{{ item.unit }}</p>
                  </div>
                </li>
              </ul>
            </div>
            <!-- 飞行高度 -->
            <div class="draw-detail-height">
              <ul style="margin-top: 28px">
                <li v-for="(item, index) in heightInfoTop" :key="index">
                  <div class="scale-item">
                    <p class="draw-grey">{{ item.label }}</p>
                    <p class="draw-blue">{{ monitorDetail.data.data[item.key].toFixed(1) }}{{ item.unit }}</p>
                  </div>
                </li>
              </ul>
              <ul style="margin-top: 42px">
                <li v-for="(item, index) in heightInfoBot" :key="index">
                  <div class="scale-item">
                    <p class="draw-grey">{{ item.label }}</p>
                    <p class="draw-blue">{{ monitorDetail.data.data[item.key].toFixed(1) }}{{ item.unit }}</p>
                  </div>
                </li>
              </ul>
            </div>
            <!-- 隔断线 -->
            <div class="draw-detail-line"></div>
            <!-- 最高点 -->
            <div class="draw-detail-peak">
              <p>最高点</p>
              <ul style="margin-top: 10px">
                <li v-for="(item, index) in heightPointInfo" :key="index">
                  <div class="scale-item">
                    <p class="draw-grey">{{ item.label }}</p>
                    <p class="draw-blue">{{ monitorDetail.data.data[item.key] }}{{ item.unit }}</p>
                  </div>
                </li>
              </ul>
            </div>
            <!-- 最低点 -->
            <div class="draw-detail-Lowest">
              <p>最低点</p>
              <ul style="margin-top: 10px">
                <li v-for="(item, index) in lowPointInfo" :key="index">
                  <div class="scale-item">
                    <p class="draw-grey">{{ item.label }}</p>
                    <p class="draw-blue">{{ monitorDetail.data.data[item.key] }}{{ item.unit }}</p>
                  </div>
                </li>
              </ul>
            </div>
          </div>
        </div>
      </div>
    </div>
    <!-- 单屏监控 -->
    <SingleScreen
      v-if="screenVisible"
      :screenVisible="screenVisible"
      :screenDetail="detailData"
      @closeScreen="closeScreen"
    ></SingleScreen>
  </div>
</template>
<script lang="ts" setup>
import {onMounted, Ref, ref, nextTick, watch, defineExpose} from 'vue';
import axios from 'axios';
import {useStore} from 'vuex';
import censusApi from '@/api/census.ts';
import SingleScreen from './SingleScreen.vue';
import UseDraggable from '@/utils/useDraggableElem';

const drawDialogVisible = ref(false);
const detailModel = ref(false);
const detailData = ref();
const screenVisible = ref(false);
const locationReg = ref('');
const locationCurrent = ref('');
const monitorDetail = ref({});
const drawRef = ref();
const store = useStore();

const props = defineProps({
  drawDialogVisible: Boolean,
  mission_id: String,
});
const emit = defineEmits(['closeDialog', 'clickPostion']);

const closeDialog = () => {
  emit('closeDialog');
};

type ListData = {
  label: string;
  key: string;
  value?: string | number;
  type?: string;
  unit?: string;
}[];
const scaleInfoTop: Ref<ListData> = ref([
  {label: 'GSD', key: 'average_ground_resolution', unit: 'cm'},
  {label: '航向重叠', key: 'longitudinal_overlap', unit: '%'},
  {label: '旁向重叠', key: 'lateral_overlap', unit: '%'},
]);
const scaleInfoBot: Ref<ListData> = ref([
  {label: '航线间距', key: 'flight_interval', unit: 'm'},
  {label: '拍照间距', key: 'take_pic_interval', unit: 'm'},
  {label: '默认空速', key: 'default_air_speed', unit: 'm/s'},
]);
const heightInfoTop: Ref<ListData> = ref([
  {label: '海拔高度', key: 'survey_average_altitude', unit: 'm'},
  {label: '飞行高度', key: 'flight_relative_altitude', unit: 'm'},
]);
const heightInfoBot: Ref<ListData> = ref([
  {label: '预计时间', key: 'work_time', unit: 'min'},
  {label: '预计面积', key: 'work_area', unit: 'km²'},
  {label: '预计里程', key: 'total_mileage', unit: 'km'},
]);
const heightPointInfo: Ref<ListData> = ref([
  {label: 'GSD', key: 'high_gsd', unit: 'cm'},
  {label: '航向重叠', key: 'high_longitudinal_overlap', unit: '%'},
  {label: '旁向重叠', key: 'high_lateral_overlap', unit: '%'},
  {label: '海拔高度', key: 'survey_highest_altitude', unit: 'm'},
]);
const lowPointInfo: Ref<ListData> = ref([
  {label: 'GSD', key: 'low_gsd', unit: 'cm'},
  {label: '航向重叠', key: 'low_longitudinal_overlap', unit: '%'},
  {label: '旁向重叠', key: 'low_lateral_overlap', unit: '%'},
  {label: '海拔高度', key: 'survey_lowest_altitude', unit: 'm'},
]);

// 监控详情
const clickDetail = () => {
  detailModel.value = true;
  getMonitorDetail();
};

const clickBack = () => {
  detailModel.value = false;
};

// 单屏监控
const openSingleScreen = () => {
  screenVisible.value = true;
};
// 关闭单屏监控
const closeScreen = () => {
  screenVisible.value = false;
};

// 定位
const clickPostion = (index) => {
  let lnglat;
  if (index === 1) {
    lnglat = [detailData.value.info.point.lon, detailData.value.info.point.lat];
  } else {
    lnglat = [detailData.value.device.longitude, detailData.value.device.latitude];
  }
  emit('clickPostion', lnglat);
};

// 地理逆编码
const findLocation = (lnglat: any) =>
  new Promise((resolve, reject) => {
    let locationText;
    AMap.plugin('AMap.Geocoder', () => {
      const geocoder = new AMap.Geocoder();
      geocoder.getAddress(lnglat, (status, result) => {
        if (status === 'complete' && result.regeocode) {
          locationText = result.regeocode;
          resolve(locationText);
        }
      });
    });
  });

// 获取详情数据
const getDetailData = async (missionId) => {
  await censusApi
    .flightDetail({mission_id: missionId})
    .then((res) => {
      detailData.value = res.data;
      findLocation([detailData.value.info.register_point.lon, detailData.value.info.register_point.lat])
        .then((data) => {
          locationReg.value = data;
        })
        .catch((err) => {
          throw err;
        });
      findLocation([detailData.value.info.point.lon, detailData.value.info.point.lat])
        .then((data) => {
          locationCurrent.value = data;
        })
        .catch((err) => {
          throw err;
        });
    })
    .catch((err) => {
      throw err;
    });
};
// 获取监控详情数据
const getMonitorDetail = async () => {
  const exportUrl = `${import.meta.env.VITE_MONITOR_URL}/flightMission/monitor`;
  const data = await axios({
    method: 'get',
    url: exportUrl,
    headers: {
      token: localStorage.getItem('token'),
    },
    params: {
      local_id: props.mission_id,
    },
  });
  monitorDetail.value = data.data;
};
// 拖拽相关
const dialogClassName = ref('dialogClassName');
let draggable;

const setUseDraggable = () => {
  dialogClassName.value += store.state.dialogClassNameCount;
  store.commit('changeDialogClassNameCount', 1);
  const params = {
    fatherSelector: dialogClassName.value,
  };
  nextTick(() => {
    draggable = new UseDraggable(params);
    const sonDom = document.getElementsByClassName(dialogClassName.value)[0];
    sonDom.parentNode.style.zIndex = store.state.dialogzIndex;
  });
};
const newDra = () => {
  const params = {
    fatherSelector: dialogClassName.value,
  };
  // eslint-disable-next-line no-new
  new UseDraggable(params);
};

const selectDialog = () => {
  try {
    const sonDom = document.getElementsByClassName(dialogClassName.value)[0];
    sonDom.parentNode.style.zIndex = store.state.dialogzIndex;
    store.commit('changeDialogzIndex', 1);
  } catch {}
};

watch(
  () => drawDialogVisible.value,
  (newVal) => {
    if (newVal && draggable !== '') {
      // 元素拖拽
      draggable = setUseDraggable();
    }
  },
);
defineExpose({getDetailData});
onMounted(() => {
  drawDialogVisible.value = props.drawDialogVisible;
  getDetailData(props.mission_id);
});
</script>
<style lang="scss" scoped>
.draw {
  width: 910px;
  height: 330px;
  position: fixed;
  top: 25%;
  left: 20%;
  background: url(@/assets/img/home/dialog_bg_big.png) no-repeat;
  background-size: 100%;

  p,
  ul,
  li {
    margin: 0;
    padding: 0;
  }
  &-con {
    width: 100%;
    height: 100%;
    box-sizing: border-box;
    padding: 30px 40px;
    &-top {
      display: flex;
      align-items: center;
      justify-content: end;
      margin-top: 17px;
      > img {
        width: 21px;
        height: 21px;
        margin-left: 7px;
        cursor: pointer;
      }
    }
    &-title {
      display: flex;
      align-items: center;
      .title-brand {
        font-size: 18px;
        font-weight: bold;
        color: #7fc9ff;
        text-shadow: 0px 2px 4px #58b9ff;
        margin-right: 15px;
      }
      .title-serial {
        font-size: 12px;
        font-weight: 400;
        color: #ffffff;
        line-height: 17px;
        opacity: 0.7;
        margin-right: 15px;
      }
      > img {
        cursor: pointer;
      }
    }
    .draw-con-line {
      height: 1px;
      background: #d8d8d8;
      margin-top: 11px;
    }
    .draw-con-main {
      margin: 20px 0;
      font-size: 13px;
      display: flex;
      justify-content: space-between;
      .postion-latlng {
        display: flex;
        align-items: center;
        margin-top: 10px;
        > img {
          width: 16px;
          height: 16px;
        }
        > p {
          color: #ffffff;
          opacity: 0.7;
          margin: 0 5px;
        }
        > span {
          color: #00d6ff;
          font-weight: bold;
        }
      }
      .postion-title {
        display: flex;
        color: #fff;
        > span {
          display: block;
          max-width: 230px;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }
        > b {
          color: #ffba00;
          text-decoration: underline;
          margin-left: 25px;

          cursor: pointer;
        }
      }
      &-left {
        margin-right: 10px;
        .postion-top,
        .postion-bottom {
          display: flex;
          &-left {
            margin-right: 30px;
          }
        }
      }
      &-right {
        background: #082f50;
        list-style: none;
        padding: 10px 16px;
        box-sizing: border-box;
        display: flex;
        margin-left: 35px;
        p {
          margin: 7px 0;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }
        &-left {
          color: #fff;
          opacity: 0.7;
          margin-right: 10px;
        }
        &-right {
          color: #00d6ff;
          > p {
            width: 230px;
          }
        }
      }
    }
    &-back {
      display: flex;
      align-items: center;
      margin-top: 10px;
      cursor: pointer;
      p {
        color: #fff;
        opacity: 0.7;
        margin-left: 5px;
      }
    }
    .draw-detail {
      display: flex;
      margin-top: 10px;
      .draw-blue {
        color: #00d6ff;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }
      .draw-grey {
        color: #fff;
        opacity: 0.7;
      }
      &-task {
        > p {
          color: #fff;
        }
        .task-device {
          margin-top: 10px;
          background: #082f50;
          height: 152px;
          padding: 10px;
          &-item {
            overflow: hidden;
            > P {
              font-weight: bold;
              margin-left: 43px;
            }
            &-img {
              display: flex;
              margin-top: 10px;
              > img {
                width: 26px;
                height: 26px;
              }
            }
          }
        }
      }
      &-scale,
      &-Lowest {
        margin-left: 26px;
      }
      &-height {
        margin-left: 20px;
      }
      &-scale,
      &-height,
      &-peak,
      &-Lowest {
        > p {
          color: #fff;
        }
        ul > li {
          margin-bottom: 5px;
        }
        .scale-item {
          display: flex;
          justify-content: space-between;
          :nth-child(1) {
            width: 70px;
            margin-right: 5px;
          }
        }
      }
      &-line {
        width: 2px;
        margin: 0 24px;
        opacity: 0.4;
        background: #d8d8d8;
      }
    }
  }
}
</style>
